package vn.keho.module.dompeto.rest;

import android.content.Context;
import android.util.Log;

import java.util.List;

import vn.keho.define.api.http.RestClient;
import vn.keho.define.api.request.ThreadedAsyncTask;
import vn.keho.define.convert.StringUtil;
import vn.keho.module.dompeto.model.ChannelModel;
import vn.keho.module.dompeto.model.PageModel;


/**
 * Created by KeHo on 7/26/14.
 */
public class DompetoRequest extends ThreadedAsyncTask<DompetoParam, Void, String> {
    private final AsyncCallbackRequestHandler handler;
    private final Context context;



    public static String mappsidonapps;
    public static String username;
    public static String token;
    public static String channelid;

    public DompetoRequest(Context context, AsyncCallbackRequestHandler handler) {
        this.handler = handler;
        this.context = context;
    }

    @Override
    protected String doInBackground(DompetoParam... params) {
        DompetoParam mapParam = params[0];

        String apiUrl = mapParam.getApiUrl();

        Log.i("REQUEST", apiUrl);
        return progressCallApi(apiUrl, mapParam);
    }

    @Override
    protected void onPostExecute(String response) {
        if (response != null && handler != null) {
            try {
                handler.handlerRequestSuccess(response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public void storeLoginAccount(DompetoParam requestParam) {
        try {
            /*DeviceUtil.writeAccountType(context, requestParam.getAccountType());
            switch (requestParam.getAccountType()) {
                case AccountType.EMAIL:
                    DeviceUtil.writeEmail(context, requestParam.getEmail());
                    DeviceUtil.writePassword(context, requestParam.getPassword());
                    break;
                case AccountType.FACEBOOK:
                    DeviceUtil.writeFacebookId(context, requestParam.getFacebookId());
                    DeviceUtil.writeFacebookToken(context, requestParam.getFacebookToken());
                    break;
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void buildHeaderRequest(RestClient restClient) {
        try {
            /*restClient.AddHeader(HeaderUtil.APP_ID, AndroidUtil.getKhAppId(context));
            restClient.AddHeader(HeaderUtil.APP_VER, AndroidUtil.getAppVersion(context) + "");
            if(StringUtil.isBlank(deviceId)) {
                //Generate DeviceId
                deviceId = DeviceUtil.getDeviceID(context);
            }
            restClient.AddHeader(HeaderUtil.DEV_ID, deviceId);
            restClient.AddHeader(HeaderUtil.DEV_TYPE, Build.MODEL);
            if(!StringUtil.isBlank(deviceToken)) {
                restClient.AddHeader(HeaderUtil.DEV_TOKEN, deviceToken);
            }
            if(!StringUtil.isBlank(authToken)) {
                restClient.AddHeader(HeaderUtil.AUTH_TOKEN, authToken);
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String progressCallApi(String url, DompetoParam requestParam) {
        RestClient restClient = new RestClient(url);
        try {
            buildHeaderRequest(restClient);

            requestParam.buildApiParam(restClient);
            restClient.ExecutePost();

            String response = restClient.getResponse();
            if (!StringUtil.isBlank(response)) {
                /*JsonResponse responseAccount = JsonUtil.generateJavaObject(response, JsonResponse.class);
                if(responseAccount.getStatus() == ResponseStatus.SUCCESS) {
                    afterCallRequestSuccess(url, requestParam, response);
                }*/
                return response;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (handler != null) {
            handler.handlerRequestError("Unknown Error");
        }

        return null;
    }

    public interface AsyncCallbackRequestHandler {
        public void handlerRequestSuccess(Object data);

        public void handlerRequestError(String message);
    }
}
